home *** CD-ROM | disk | FTP | other *** search
- /*
- * @(#)StandardCommands.java 1.1 95/05/10
- *
- * Copyright (c) 1995 Sun Microsystems, Inc. All Rights reserved Permission to
- * use, copy, modify, and distribute this software and its documentation for
- * NON-COMMERCIAL purposes and without fee is hereby granted provided that
- * this copyright notice appears in all copies. Please refer to the file
- * copyright.html for further important copyright and licensing information.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
- * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
- * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
- * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
- * ITS DERIVATIVES.
- */
-
- package java.tools.jsc.commands;
- import java.tools.jsc.*;
-
- /**
- * A class to implement the standard script commands
- * @author James Gosling
- */
-
- public class StandardCommands implements ScriptCommand {
- public StandardCommands (ScriptContext ctx) {
- ctx.defun("!", this);
- ctx.defun("$", this);
- ctx.defun("%", this);
- ctx.defun("&", this);
- ctx.defun("*", this);
- ctx.defun("+", this);
- ctx.defun("-", this);
- ctx.defun("/", this);
- ctx.defun("<", this);
- ctx.defun("=", this);
- ctx.defun(">", this);
- ctx.defun("^", this);
- ctx.defun("|", this);
- ctx.defun("~", this);
- ctx.defun("and", this);
- ctx.defun("or", this);
- ctx.defun("for", this);
- ctx.defun("if", this);
- ctx.defun("puts", this);
- ctx.defun("set", this);
- ctx.defun("while", this);
- }
- public Object eval(ScriptContext ctx) {
- int i;
- int limit = ctx.nargs();
- switch (ctx.command().charAt(0)) { /* Gross hack! */
- case '!':
- return ctx.evarg(0) == null ? "true" : null;
- case '~':
- return new Integer(~ctx.arg(0, 0));
- case '$':
- return ctx.get(ctx.arg(0, "V"));
- case '&':{
- int v = ctx.arg(0, 0);
- for (i = 1; i < limit; i++)
- v &= ctx.arg(i, 0);
- return new Integer(v);
- }
- case '|':{
- int v = ctx.arg(0, 0);
- for (i = 1; i < limit; i++)
- v |= ctx.arg(i, 0);
- return new Integer(v);
- }
- case '^':{
- int v = ctx.arg(0, 0);
- for (i = 1; i < limit; i++)
- v ^= ctx.arg(i, 0);
- return new Integer(v);
- }
- case '+':{
- double v = ctx.arg(0, 0.0);
- for (i = 1; i < limit; i++)
- v += ctx.arg(i, 0.0);
- return new Double(v);
- }
- case '-':{
- double v = ctx.arg(0, 0.0);
- for (i = 1; i < limit; i++)
- v -= ctx.arg(i, 0.0);
- return new Double(v);
- }
- case '*':{
- double v = ctx.arg(0, 0.0);
- for (i = 1; i < limit; i++)
- v *= ctx.arg(i, 0.0);
- return new Double(v);
- }
- case '/':{
- double v = ctx.arg(0, 0.0);
- for (i = 1; i < limit; i++)
- v /= ctx.arg(i, 0.0);
- return new Double(v);
- }
- case '%':{
- double v = ctx.arg(0, 0.0);
- for (i = 1; i < limit; i++)
- v %= ctx.arg(i, 0.0);
- return new Double(v);
- }
- // case '<':{
- // double v = ctx.arg(0, 0.0);
- // for (i = 1; i < limit; i++)
- // ret X = ctx.arg(i, 0);
- // }
- // break;
- case '=':{
- Object o = ctx.evarg(0);
- if (o == null)
- for (i = 1; i < limit; i++) {
- if (ctx.evarg(i) != null)
- return null;
- }
- else
- for (i = 1; i < limit; i++)
- if (!o.equals(ctx.arg(i)))
- return null;
- return "true";
- }
- // case '>':{
- // double v = ctx.arg(0, 0.0);
- // for (i = 1; i < limit; i++)
- // ret X = ctx.arg(i, 0);
- // }
- // break;
- case 'a':{ /* and */
- Object o = null;
- for (i = 0; i < limit; i++)
- if ((o = ctx.arg(i)) == null)
- return null;
- return o;
- }
- case 'o':{ /* or */
- Object o = null;
- for (i = 0; i < limit; i++)
- if ((o = ctx.arg(i)) != null)
- return null;
- return o;
- }
- case 'f': /* for */
- {
- Object o = null;
- for (ctx.evarg(0); ctx.evarg(1) != null; ctx.evarg(2))
- for (i = 3; i < limit; i++)
- o = ctx.evarg(i);
- return o;
- }
- case 'i': /* if */
- return ctx.evarg(ctx.evarg(0) == null ? 2 : 1);
- case 'p': /* puts */
- for (i = 0; i < limit; i++)
- System.out.print(ctx.arg(i, ""));
- return null;
- case 's': /* set */
- {
- Object v = ctx.evarg(1);
- ctx.put(ctx.arg(0, "V"), v);
- return v;
- }
- case 'w': /* while */
- {
- Object o = null;
- while (ctx.evarg(0) != null)
- for (i = 1; i < limit; i++)
- o = ctx.evarg(i);
- return o;
- }
- }
- return null;
- }
- }
-